package com.example.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.Map;

/**
 * @author 贾明祯
 * @ClassName JedisUtil
 * @Date 2020/8/18 19:15
 * @Introduce
 */
public class JedisUtil {
    private Jedis jedis;
    /**
     * 5分钟
     */
    private static final int MINUTES = 60*5;
    public JedisUtil(){
        JedisPool pool = getPool();
        jedis=pool.getResource();
    }
    public static JedisPool getPool(){
        JedisPool pool=null;
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(Integer.parseInt("50"));
        config.setMaxTotal(Integer.parseInt("1000"));
        config.setMaxWaitMillis((long)Integer.parseInt("3000"));
        config.setTestOnBorrow(Boolean.parseBoolean("true"));
        config.setTestOnReturn(Boolean.parseBoolean("true"));
        config.setTimeBetweenEvictionRunsMillis(-1L);
        String ip="127.0.0.1";
        int port=6379;
        String password="redispass";
        pool=new JedisPool(config,ip,port);
        return pool;
    }

    /**
     * @Author: 贾明祯
     * @Date: 2020/8/19 18:31
     * @Introduce: 设置Object的String缓存
     */
    public static void setString(String key,Object obj){
        JedisUtil.getPool().getResource().set(key, JSONObject.toJSONString(obj));
    }

    /**
     * @Author: 贾明祯
     * @Date: 2020/8/19 18:32
     * @Introduce: 设置String类型缓存
     */
    public static void setString(String key,String str){
        JedisUtil.getPool().getResource().set(key, str);
    }

    /**
     * @Author: 贾明祯
     * @Date: 2020/8/19 18:34
     * @Introduce: 获取String类型缓存
     */
    public static String getString(String key){
        String str = JedisUtil.getPool().getResource().get(key);
        if(str!=null){
            return str;
        }
        return null;
    }

    /**
     * @Author: 贾明祯
     * @Date: 2020/8/19 18:28
     * @Introduce: 获取String缓存中的Object
     */
    public static Object getObj(String key,Class c){
        String str = JedisUtil.getPool().getResource().get(key);
        Object obj = JSONArray.parseObject(str,c);
        return obj;
    }

    /**
     * @Author: 贾明祯
     * @Date: 2020/8/19 18:52
     * @Introduce: 设置Hash类型缓存
     */
    public static void setHash(String key,Map<String,String> map){
        JedisUtil.getPool().getResource().hset(key,map);
    }

    /**
     * @Author: 贾明祯
     * @Date: 2020/8/19 18:58
     * @Introduce: 拿取Hash中对应的字符串
     */
    public static String getString(String key,String field){
        String str = JedisUtil.getPool().getResource().hget(key, field);
        if(str!=null){
            return str;
        }
        return null;
    }
    /**
     * @Author: 贾明祯
     * @Date: 2020/8/19 18:26
     * @Introduce: 拿取hash缓存的对象
     */
    public static Object getObj(String key,String field,Class c){
        String str  = JedisUtil.getPool().getResource().hget(key, field);
        Object obj = JSONArray.parseObject(str,c);
        return obj;
    }

    /**
      * @Author: 贾明祯
      * @Date: 2020/8/21 9:21
      * @Introduce: 删除缓存
      */
    public static boolean delKey(String key){
        JedisUtil.getPool().getResource().del(key);
        return true;
    }

    /**
      * @Author: 贾明祯
      * @Date: 2020/8/21 9:18
      * @Introduce: 设置缓存时间
      */
    public static boolean setKeyTime(String key){
        JedisUtil.getPool().getResource().expire(key, 60);
        return true;
    }

    /**
     * @Author: 贾明祯
     * @Date: 2020/8/21 9:18
     * @Introduce: 设置缓存时间
     * @param seconds 秒为单位
     */
    public static boolean setKeyTime(String key,int seconds){
        JedisUtil.getPool().getResource().expire(key, seconds);
        return true;
    }

}
